package com.zhilianbao.leyaogo.http.callback;

import android.content.Context;

import com.bql.utils.CheckUtils;
import com.zhilianbao.leyaogo.view.dialog.LeProgressDialog;
import com.zhilianbao.okhttputils.request.BaseRequest;

/**
 * ClassName: DialogCallback <br>
 * Description: 对话框Callback<br>
 * Author: Cyarie <br>
 * Created: 2016/7/20 14:44 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public abstract class DialogCallback<T> extends JsonCallback<T> {

    private LeProgressDialog mProgressDialog;//进度对话框

    private String loadingText = "" /*LeYaoGoApplication.getContext().getString(R.string.loading_data)*/;


    public DialogCallback(Context context, String loadingText) {
        this(context);
        this.loadingText = loadingText;
    }


    public DialogCallback(Context context) {
        super(context);
        initPro(context);
    }

    public DialogCallback(Context context,boolean isShowNetToast) {
        super(context);
        this.isShowNetToast = isShowNetToast;
        initPro(context);
    }


    /**
     * 初始化进度条对话框
     *
     * @param context
     */
    public void initPro(Context context) {
        if (mProgressDialog == null)
            mProgressDialog = new LeProgressDialog(context);

    }


    /**
     * 显示进度对话框
     *
     * @param text 显示文本
     */
    public void showPro(String text) {
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
            if (!CheckUtils.isEmpty(text)) {
                mProgressDialog.showText(text);
                mProgressDialog.setPadding();
            } else {
                mProgressDialog.hideText();
                mProgressDialog.setPaddingWithouotText();
            }

        }
    }

    /**
     * dismiss进度对话框
     */
    public void dismissPro() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.cancelProgressBar();
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void onBefore(BaseRequest request) {
        super.onBefore(request);
        showPro(loadingText);
    }

    @Override
    public void onAfter(T t, Exception e) {
        super.onAfter(t, e);
        if (dismissDialog()) {
            dismissPro();
        }
    }

    /**
     * 关闭加载对话框 默认true
     *
     * @return
     */
    public boolean dismissDialog() {
        return true;
    }

}
